<?php

namespace zhtfrontend\models\datas;

use Yii;

/**
 * This is the model class for table "{{%class_lesson}}".
 *
 * @property integer $lesson_id
 * @property integer $class_id
 * @property integer $class_date
 * @property integer $start_time
 * @property integer $end_time
 * @property integer $lesson
 * @property string $title
 * @property integer $status
 * @property integer $ovo
 * @property string $info
 * @property integer $postpone
 * @property integer $product_id
 */
class ClassLesson extends \zhtfrontend\components\bases\BaseModel
{
    public $change_time;
    public $oldlesson;
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%class_lesson}}';
    }

    public $postpone;

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['class_id', 'lesson', 'status', 'ovo','product_id','is_delay','oldlesson'], 'integer'],
            [['info','lesson_type','remark','change_time'], 'string'],
            [['title','start_time','end_time'], 'string', 'max' => 50],
            [['class_date','remark'], 'required','on' => ['updateclasstime']],
            [['remark','status'], 'required','on'=>['status']],
            [['is_delay'],'required','when'=>function(){
                return $this->status == 5;
            }],
            [['is_delay'],'required','when'=>function(){
                return $this->status == 6;
            }],
            [['change_time'],'required','when'=>function(){
                return $this->status == 7;
            }],
            ['class_date','check_date','on'=>'updateclasstime'],
            ['lesson','check_lesson','on'=>'updateclasstime']
//            [['teacher'], 'required','on' => ['updateteacher']],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'lesson_id' => Yii::t('zhtfrontend', '课次序号'),
            'class_id' => Yii::t('zhtfrontend', '班级ID'),
            'class_date' => Yii::t('zhtfrontend', '上课日期'),
            'start_time' => Yii::t('zhtfrontend', '开始上课时间'),
            'end_time' => Yii::t('zhtfrontend', '结束上课时间'),
            'lesson' => Yii::t('zhtfrontend', '课次'),
            'title' => Yii::t('zhtfrontend', '上课日期'),
            'status' => Yii::t('zhtfrontend', '状态'),
            'ovo' => Yii::t('zhtfrontend', '授课形式(0一对多，1一对一)'),
            'info' => Yii::t('zhtfrontend', '课时其他信息'),
            'product_id' => Yii::t('zhtfrontend', '产品id'),
            'postpone' => Yii::t('zhtfrontend', '课次顺延'),
            'lesson_type' => Yii::t('zhtfrontend', '课次类型'),
            'remark' => Yii::t('zhtfrontend', '备注'),
            'is_delay' => Yii::t('zhtfrontend', '是否延课'),
            'change_time' => Yii::t('zhtfrontend', '调换时间'),
            'oldlesson' => Yii::t('zhtfrontend', '旧课次'),
        ];
    }

    public function fields()
    {
        $fields = parent::fields();
        $fields['class_date'] = function () {
            return \Yii::$app->formatter->asDate($this->class_date);
        };
        $fields['status'] = function () {
            switch ($this->status) {
                case 0:
                    return \Yii::t('zhtfrontend', '未上课');
                case 1:
                    return \Yii::t('zhtfrontend', '已上课');
                case 2:
                    return \Yii::t('zhtfrontend', '教师请假');
                case 3:
                    return \Yii::t('zhtfrontend', '学生请假');
                case 4:
                    return \Yii::t('zhtfrontend', '教师停课');
                case 5:
                    return \Yii::t('zhtfrontend', '代课');
                case 6:
                    return \Yii::t('zhtfrontend', '复课');
            }
        };

        return $fields;
    }

    public function extraFields()
    {
        $fields = parent::extraFields(); // TODO: Change the autogenerated stub
        $fields['course_time'] = function (){
            $date = \Yii::$app->formatter->asDatetime($this->class_date);
            $start_time = $this->start_time;
            $end_time = $this->end_time;
            $len = strlen($start_time) - 2;
            $lenth = strlen($end_time) - 2;
            $start_time = ($len == 1) ? ('0' . floor($start_time / 100) . ':' . substr($start_time, 1)) : (floor($start_time / 100) . ':' . substr($start_time, 2));
            $end_time = ($lenth == 1) ? ('0' . floor($end_time / 100) . ':' . substr($end_time, 1)) : (floor($end_time / 100) . ':' . substr($end_time, 2));
            $time = (empty($date)) ? ($start_time . '-' . $end_time) : ($date . ' ' . $start_time . '-' . $end_time);
            return $time;
        };

        $fields['teacher_name'] = function (){
            $goods = isset($this->classManage)?$this->classManage->goods:null;
            if(!empty($goods)){
                $arr = Arrangement::findOne($goods->arrangement_id);
                if(!empty($arr)){
                    return isset($arr->teacher)?$arr->teacher->name:null;
                }
            }
            return null;
        };
        //为消息产生的字段内容
        $fields['all_num'] = function(){
            $all_num = StudentCheck::find()->where(['class_lesson_id'=>$this->lesson_id])->count();
            return $all_num;
        };
        //请假的学员数目
        $fields['leave_num'] = function(){
            $leave_num = StudentCheck::find()->where(['class_lesson_id'=>$this->lesson_id,'status'=>2])->count();
            return $leave_num;
        };
        //签到的学员数目
        $fields['punch_num'] = function(){
            $punch_num = StudentCheck::find()->where(['class_lesson_id'=>$this->lesson_id,'status'=>1])->count();
            return $punch_num;
        };
        //未签到的学员数目
        $fields['nopunch_info'] = function(){
            $customer_arr = [];
            $nopunch_infos = StudentCheck::find()->where(['class_lesson_id'=>$this->lesson_id,'status'=>0])->all();
            foreach($nopunch_infos as $ni){
                if(isset($ni->student) && isset($ni->student->customer)){
                    $customer_arr[] = $ni->student->customer->name;
                }
            }
            if(!empty($customer_arr)){
                return implode(',',$customer_arr);
            }else{
                return '没有';
            }
        };

        return $fields;
    }

    public function getClassManage()
    {
        return $this->hasOne(ClassManage::className(), ['class_id' => 'class_id']);
    }

    public function check_lesson($attribute){
          $lesson = ClassLesson::find()->where(['class_id'=>$this->class_id])->andWhere(['<','class_date',time()])->orderBy('lesson DESC')->one();
          if($lesson!= null && $lesson->lesson >= $this->$attribute){
              $this->addError($attribute, Yii::t('zhtfrontend', '此课次已上！'));
          }
          if($this->lesson > $this->oldlesson){
              $this->addError($attribute, Yii::t('zhtfrontend', '设置的课次小于本身的课次!'));
          }
    }

    public function check_date($attribute){
        $lesson = ClassLesson::find()->where(['class_id'=>$this->class_id])->andWhere(['<','class_date',time()])->orderBy('lesson DESC')->one();
        $tiem = strtotime($this->$attribute.'23:59:59');
        if($lesson!= null && $lesson->class_date > $tiem){
            $this->addError($attribute, Yii::t('zhtfrontend', '此日期的课已上！'));
        }
    }

    public function afterDelete(){
        parent::afterDelete();
        StudentCheck::deleteAll(['class_lesson_id'=>$this->lesson_id]);
        LessonStudent::deleteAll(['lesson_id'=>$this->lesson_id]);
    }
}
